<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>string</title>
</head>
<body>
<script>
  const string1 = `imqt3 wokdl35
    h3qdhl3
    imqt3
    wom3
    D
  `;
  const string2 = `
    imqt3 wokdl35
    h3qdhl3
    imqt3
    wom3
    D
  `;
  const string3 = (
    `imqt3 wokdl35
    h3qdhl3
    imqt3
    wom3
    D`
  );
  const string4 = `imqt3 wokdl35
                  h3qdhl3
                  imqt3
                  wom3
                  D
  `;
  const string5 = `<div>
                      <img src="/wokdl35/sss.jpg"/>
                      <p>h3qdhl3</p>
                      <ul>
                        <li>imqt3</li>
                        <li>h3qdhl3</li>
                        <li>wom3</li>
                      </ul>
                    </div>
  `;

  /**
   * 格式化字符串模板
   *
   * @param { string } string
   * @param number: trim number
   * @returns string
   */
  function formatTmpString(string, number = '') {
    const regExp = new RegExp(`\\n\\s{1,${ number }}`, 'g');
    return string.replace(regExp, '\n')
  }

  console.log(formatTmpString(string1));
  console.log(formatTmpString(string2));
  console.log(formatTmpString(string3));
  console.log(formatTmpString(string4));
  console.log(formatTmpString(string5, 20));
</script>
</body>
</html>